package com.baidu.cleaner;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.apache.commons.lang3.StringUtils;

public class TextFileCleaner extends BaseCleaner {
	private String inputFilePath;
	private String inputFileEncoding;
	private String inputFieldSeparator;
	private String outputFilePath;
	private String outputFileEncoding;
	private String outputFieldSeparator;
	private String validatorId;
	private BufferedReader br = null;
	private BufferedWriter bw = null;
	private BufferedWriter logWriter = null;

	public TextFileCleaner(String inputFilePath, String inputFileEncoding, String inputFieldSeparator, String outputFilePath, String outputFileEncoding, String outputFieldSeparator, String logFilePath, String validatorId)
			throws Exception {
		super(inputFieldSeparator, outputFieldSeparator, validatorId);
		String encoding = StringUtils.isBlank(inputFileEncoding) == true ? this.table.getEcoding() : inputFileEncoding;
		br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilePath), encoding));
		this.bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFilePath), outputFileEncoding));
		this.logWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFilePath), "UTF-8"));
	}

	public String getInputFilePath() {
		return inputFilePath;
	}

	public void setInputFilePath(String inputFilePath) {
		this.inputFilePath = inputFilePath;
	}

	public String getInputFileEncoding() {
		return inputFileEncoding;
	}

	public void setInputFileEncoding(String inputFileEncoding) {
		this.inputFileEncoding = inputFileEncoding;
	}

	public String getInputFieldSeparator() {
		return inputFieldSeparator;
	}

	public void setInputFieldSeparator(String inputFieldSeparator) {
		this.inputFieldSeparator = inputFieldSeparator;
	}

	public String getOutputFilePath() {
		return outputFilePath;
	}

	public void setOutputFilePath(String outputFilePath) {
		this.outputFilePath = outputFilePath;
	}

	public String getOutputFileEncoding() {
		return outputFileEncoding;
	}

	public void setOutputFileEncoding(String outputFileEncoding) {
		this.outputFileEncoding = outputFileEncoding;
	}

	public String getOutputFieldSeparator() {
		return outputFieldSeparator;
	}

	public void setOutputFieldSeparator(String outputFieldSeparator) {
		this.outputFieldSeparator = outputFieldSeparator;
	}

	public String getValidatorId() {
		return validatorId;
	}

	public void setValidatorId(String validatorId) {
		this.validatorId = validatorId;
	}

	@Override
	public String getLine() throws Exception {
		String line = null;
		try {
			line = this.br.readLine();
		} catch (IOException e) {
			close();
			throw e;
		}

		return line;
	}

	@Override
	public void writeLine(String line) throws Exception {
		try {
			this.bw.write(line);
			this.bw.write(System.getProperty("line.separator"));
		} catch (Exception e) {
			close();
			throw e;
		}
	}

	@Override
	protected void writeLog(String msg) throws Exception {
		try {
			this.logWriter.write(msg);
			this.logWriter.write(System.getProperty("line.separator"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void close() {
		if (this.br != null) {
			try {
				this.br.close();
			} catch (IOException e) {
				this.br = null;
				e.printStackTrace();
			}
		}

		if (this.bw != null) {
			try {
				this.bw.close();
			} catch (IOException e) {
				this.bw = null;
				e.printStackTrace();
			}
		}
		
		if (this.logWriter != null) {
			try {
				this.logWriter.close();
			} catch (IOException e) {
				this.logWriter = null;
				e.printStackTrace();
			}
		}
	}
}
